{%- set etcdnode=[] -%}
{%- if groups['etcd'] is not defined %}
    {%- for host in groups['master'] -%}
        {{ etcdnode.append(host) }}
    {%- endfor%}
{%- else%}
    {%- for host in groups['etcd'] -%}
        {{ etcdnode.append(host) }}
    {%- endfor%}
{%- endif %}

[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes

[Service]
ExecStart=/usr/local/bin/kube-apiserver \
  --advertise-address={{ ansible_default_ipv4.address }} \
  --allow-privileged=true \
  --client-ca-file=/etc/kubernetes/pki/ca.crt \
  --enable-bootstrap-token-auth \
  --storage-backend=etcd3 \
  --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt \
  --etcd-certfile=/etc/kubernetes/pki/apiserver-etcd-client.crt \
  --etcd-keyfile=/etc/kubernetes/pki/apiserver-etcd-client.key \
  --etcd-servers={% for host in etcdnode %}https://{{ host }}:2379{% if not loop.last %},{% endif %}{% endfor %} \
  --insecure-port=0 \
  --kubelet-client-certificate=/etc/kubernetes/pki/apiserver-kubelet-client.crt \
  --kubelet-client-key=/etc/kubernetes/pki/apiserver-kubelet-client.key \
  --proxy-client-cert-file=/etc/kubernetes/pki/front-proxy-client.crt \
  --proxy-client-key-file=/etc/kubernetes/pki/front-proxy-client.key \
  --requestheader-allowed-names=front-proxy-client \
  --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.crt \
  --requestheader-extra-headers-prefix=X-Remote-Extra- \
  --requestheader-group-headers=X-Remote-Group \
  --requestheader-username-headers=X-Remote-User \
  --secure-port=6443 \
  --service-account-issuer=https://kubernetes.default.svc.cluster.local \
  --service-account-key-file=/etc/kubernetes/pki/sa.pub \
  --service-account-signing-key-file=/etc/kubernetes/pki/sa.key \
  --service-cluster-ip-range={{ networking.serviceSubnet | default('10.96.0.0/12') }} \
  --tls-cert-file=/etc/kubernetes/pki/apiserver.crt \
  --tls-private-key-file=/etc/kubernetes/pki/apiserver.key \
  --bind-address={{ apiServer.bindAddress | default('0.0.0.0') }}  \
  --audit-log-path=/var/log/audit.log \
  --audit-policy-file=/etc/kubernetes/audit-policy-minimal.yaml \
  --audit-log-format=json \
  --audit-log-maxage=1 \
  --audit-log-maxbackup=3 \
  --audit-log-maxsize=200 \
  --experimental-encryption-provider-config=/etc/kubernetes/encryption-config.yaml \
{% if apiServer.admissionControlConfigFile is defined %}
  --admission-control-config-file=/etc/kubernetes/admission-control-config.yaml \
{% endif %}
{% if apiServer.extraArgs is defined %}
{% for extraArg in apiServer.extraArgs %}
  {{ extraArg }} \
{% endfor %}
{% endif %}
  --v={{ apiServer.log.level | default(1) }}

Restart=on-failure
RestartSec=5
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
